import os
from PIL import Image

input_root = '20230814'
output_root = '20230814_960_540'

def rotate_and_resize_image(image_path, output_path):
    try:
        image = Image.open(image_path)
        rotated_image = image.transpose(Image.ROTATE_90)  # 顺时针旋转90度
        resized_image = rotated_image.resize((960, 540))  # 调整尺寸为640x480
        output_path_with_extension = os.path.splitext(output_path)[0] + '.jpg'
        resized_image.save(output_path_with_extension, format='JPEG', quality=95)  # 保存为JPEG格式，设置保存质量为95
    except Exception as e:
        print(f"Error processing {image_path}: {e}")

def process_folders(root_folder):
    for foldername in os.listdir(root_folder):
        folder_path = os.path.join(root_folder, foldername)
        if os.path.isdir(folder_path):
            output_folder = os.path.join(output_root, foldername)
            os.makedirs(output_folder, exist_ok=True)

            for filename in os.listdir(folder_path):
                if filename.lower().endswith('.jpg') or filename.lower().endswith('.jpeg'):
                    input_path = os.path.join(folder_path, filename)
                    output_path = os.path.join(output_folder, filename)
                    rotate_and_resize_image(input_path, output_path)

if __name__ == "__main__":
    process_folders(input_root)
